package com.vcms.view.simulation.customer;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.vcms.controller.customer.CoinReceiver;
import com.vcms.controller.customer.TransactionController;
import com.vcms.view.simulation.system.SimulatorControlPanel;
import com.vcms.view.simulation.uicomponent.display.LabelledDisplay;
import com.vcms.view.simulation.uicomponent.listener.TerminateButtonListener;
import com.vcms.view.simulation.uicomponent.listener.WarningDisplay;

@SuppressWarnings("serial")
public class CustomerPanel extends Dialog {

	private static final String TITLE = "Customer Panel";
	public static final char DIALOG = 'a';
	
	public final static Color ACT_COLOR = Color.white;

	private TransactionController transactionController;

	private CoinInputBox coinInput;
	private DrinkSelectionBox drinkInput;

	private LabelledDisplay moneyInserted;
	private LabelledDisplay collectCoin;
	private LabelledDisplay collectCan;
	private WarningDisplay invalidCoinWarningLabel;
	private WarningDisplay noChangeAvailableWarningLabel;
	private Button terminateBtn;

	public CustomerPanel(Frame fr, TransactionController transactController, char mode) {
		super(fr, TITLE, false);
		this.transactionController = transactController;

		Label titleLabel = new Label(TITLE);
		titleLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
//		Panel tp1 = new Panel();
//		tp1.add(lb);

		/*Label lb = new Label("VIMTO Soft Drink Dispenser");
		lb.setFont(new Font("Helvetica", Font.BOLD, 24));
		lb.setAlignment(Label.CENTER);*/

		invalidCoinWarningLabel = new WarningDisplay("Invalid Coin");
		noChangeAvailableWarningLabel = new WarningDisplay("No Change Available");
		
		moneyInserted = new LabelledDisplay("Total Money Inserted:", 10, LabelledDisplay.FLOW);
		moneyInserted.setEditable(false);
		moneyInserted.setTextBackground(Color.WHITE);
		moneyInserted.setTextForeground(Color.BLACK);
		
		collectCoin   = new LabelledDisplay("Collect Coins:   ",  5, LabelledDisplay.FLOW);
		collectCoin.setEditable(false);
		collectCoin.setTextBackground(Color.WHITE);
		collectCoin.setTextForeground(Color.BLACK);
		
		collectCan    = new LabelledDisplay("Collect Can Here:", 10, LabelledDisplay.FLOW);
		collectCan.setEditable(false);
		collectCan.setTextBackground(Color.WHITE);
		collectCan.setTextForeground(Color.BLACK);
		
		this.initializeCoins();
		this.initializeDrinks(mode);
		
		// TOP PANEL FOR COIN AND DRINK
		Panel coinPanel = new Panel();
		coinPanel.setLayout(new GridLayout(3,1));
		coinInput.setActive(false);
		coinPanel.add(coinInput);
		coinPanel.add(invalidCoinWarningLabel);
		coinPanel.add((Panel)moneyInserted.getLabelledDisplay());
//		coinPanel.add(noChangeAvailableWarningLabel);
		
		
		Panel drinkPanel = new Panel();
		drinkPanel.setLayout(new GridLayout(1,1));
		drinkPanel.add(drinkInput);
		
		
		Panel topPanel = new Panel();
		topPanel.setLayout(new BorderLayout());
		topPanel.add("North", titleLabel);
		topPanel.add("Center",coinPanel);
		topPanel.add("South",drinkPanel);
		
		
		// boottom panel for terminate button
		
		terminateBtn = new Button("Terminate and Return Cash");
		terminateBtn.setFont(new Font("Helvetica", Font.BOLD, 12));
		terminateBtn.setBackground(ACT_COLOR);
		terminateBtn.addActionListener(new TerminateButtonListener(transactionController));
		
		Panel bottomPanel = new Panel();
		bottomPanel.setLayout(new GridLayout(5,1));
		bottomPanel.add(noChangeAvailableWarningLabel);
		bottomPanel.add(terminateBtn);
		bottomPanel.add((Panel)collectCoin.getLabelledDisplay());	
		bottomPanel.add((Panel)collectCan.getLabelledDisplay());	

		// layout setup
		this.setLayout(new BorderLayout());
		this.add("North", topPanel);
		this.add("Center", bottomPanel);

		pack();
		setLocation(200, 100);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				transactionController.closePanel();
			}
		});

	}

	public void display() {
		this.setVisible(true);
	}
	
	private void initializeCoins () {
		CoinReceiver coinRcv = transactionController.getCoinReceiver();
		coinInput = new CoinInputBox(this, coinRcv);
		coinInput.addListener();
	}
	
	private void initializeDrinks(char mode) {
		switch(mode) {
		case SimulatorControlPanel.ACT_CUSTOMER:
			drinkInput = new SimpleDrinkSelectionBox(this, transactionController);
			break;
		case SimulatorControlPanel.ACT_CUSTOMER_NUMERIC:
			drinkInput = new NumericDrinkSelectionBox(this, transactionController);
			break;
		case SimulatorControlPanel.ACT_CUSTOMER_ALPHANUMERIC:
			drinkInput = new AlphanumericDrinkSelectionBox(this, transactionController);
			break;
		}
	}

	public TransactionController getTransactionController() {return transactionController;}
	public CoinInputBox getCoinInput() {return coinInput;}
	public DrinkSelectionBox getDrinkInput() {return drinkInput;}
	public LabelledDisplay getMoneyInserted() {return moneyInserted;}
	public LabelledDisplay getCollectCoin() {return collectCoin;}
	public LabelledDisplay getCollectCan() {return collectCan;}
	public WarningDisplay getInvalidCoinWarningLabel() {return invalidCoinWarningLabel;}
	public WarningDisplay getNoChangeAvailableWarningLabel() {return noChangeAvailableWarningLabel;}
	public Button getTerminateBtn() {return terminateBtn;}
	
}
